/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.controller.comment;

import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.annotation.NoRepeatSubmit;
import com.shop.cereshop.app.annotation.NoRepeatWebLog;
import com.shop.cereshop.app.page.comment.*;
import com.shop.cereshop.app.page.product.BuyerComment;
import com.shop.cereshop.app.param.comment.*;
import com.shop.cereshop.app.service.shop.CereShopCommentService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.GsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 我的评价
 */
@RestController
@RequestMapping("comment")
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "CommentController")
@Api(value = "我的评价", tags = "我的评价")
public class CommentController {

    @Autowired
    private CereShopCommentService cereShopCommentService;

    @GetMapping("getAll")
    @ApiOperation(value = "我的评价列表查询")
    public Result<CommentCount> getAll(CommentParam param, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        CommentCount comment = cereShopCommentService.getAll(param, user);
        return new Result(comment, CoReturnFormat.SUCCESS);
    }

    @GetMapping("getCommentList")
    @ApiOperation(value = "我的评价列表查询")
    public Result<CommentCount> getCommentList(CommentParam param, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        CommentCount comment = cereShopCommentService.getCommentList(param, user);
        return new Result(comment, CoReturnFormat.SUCCESS);
    }

    @GetMapping("getProductAll")
    @ApiOperation(value = "商品评价列表查询")
    public Result<Page<WordComment>> getProductAll(CommentWorParam param) throws CoBusinessException, Exception {
        Page page = cereShopCommentService.getProductAll(param);
        return new Result(page, CoReturnFormat.SUCCESS);
    }

    @GetMapping("getProductComments")
    @ApiOperation(value = "查询商品评论列表")
    public Result<Page<BuyerComment>> getProductComments(CommentWordDTO param, HttpServletRequest request) {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        Page<BuyerComment> page = cereShopCommentService.getProductComments(param, user);
        return new Result<>(page, CoReturnFormat.SUCCESS);
    }

    @ApiOperation(value = "查询商品评论统计（新版）")
    @GetMapping("/product/commentCount/{productId}")
    public Result<CommentCount> getProductCommentStatistics(@PathVariable("productId") @NotNull Long productId) {
        return cereShopCommentService.getProductCommentStatistics(productId);
    }

    @ApiOperation(value = "查询商品评论列表（新版）")
    @GetMapping("/product/comments")
    public Result<PageInfo<ProductCommentVO>> getProductCommentList(CommentWordDTO param, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        PageInfo<ProductCommentVO> page = cereShopCommentService.getProductCommentList(param, user.getBuyerUserId());
        return new Result<>(page, CoReturnFormat.SUCCESS);
    }

    @GetMapping("getProdCommentFilter/{productId}")
    @ApiOperation(value = "商品评价过滤条件查询")
    public Result<CommentCount> getProductCommentFilter(@PathVariable("productId") @NotNull Long productId) {
        CommentCount commentCount = cereShopCommentService.getProductCommentFilter(productId);
        return new Result<>(commentCount, CoReturnFormat.SUCCESS);
    }

    @GetMapping("getById")
    @ApiOperation(value = "评论详情查询")
    public Result<ShopCommentVO> getById(CommentIdParam param, HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        ShopCommentVO comment = cereShopCommentService.getById(param.getCommentId(), user.getBuyerUserId());
        return new Result<>(comment, CoReturnFormat.SUCCESS);
    }

    @PostMapping("addComment")
    @ApiOperation(value = "添加评论")
    public Result addComment(@NotNull @RequestBody CommentAddDTO param, HttpServletRequest request) throws CoBusinessException {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        cereShopCommentService.addComment(param, user);
        return new Result(CoReturnFormat.SUCCESS);
    }

    @PostMapping("like")
    @ApiOperation(value = "点赞评论")
    @NoRepeatSubmit
    @NoRepeatWebLog
    public Result like(@RequestBody LikeParam param, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        cereShopCommentService.like(param, user);
        return new Result(CoReturnFormat.SUCCESS);
    }
}
